package com.base.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.base.component.Constant;

public class AuthorityFilter implements Filter {
	
	private static String ROOT;
	
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse resp = (HttpServletResponse)response;
		Object account = req.getSession().getAttribute(Constant.ACCOUNT_ID);
		if(account != null){
			chain.doFilter(request, response);
		}else{
			resp.sendRedirect(ROOT+Constant.PAGE_LOGIN);
		}
	}

	
	public void init(FilterConfig config) throws ServletException {
		ROOT = config.getServletContext().getContextPath();
	}
	public void destroy() {

	}


}
